package gui;

import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

import java.awt.Dimension;
import javax.swing.border.EtchedBorder;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;

import brac.*;
import fn.*;

import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.JCheckBox;

public class BracketingOutput extends JPanel {
	private static final long serialVersionUID = 806555713815892709L;
	
	public JButton btnPlay;
	public JButton btnStop;
	public JScrollPane scrollPane;
	public OutputTable outputTable;
	boolean isPlaying = false;
	public JSpinner spnDelay;
	public JCheckBox chckbxLoops;
	private PlotCanvas p;

	public static void main(String[] args) throws Exception {
		Function fn = Subtract.create(Multiply.create(Variable.create(), Sine.create(Variable.create())), Constant.create(1));
		Bisection b = new Bisection();
		b.solve(fn, 0.0012166, 50, 0, 2);
		//
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		JFrame f = new JFrame();
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		BracketingOutput p = new BracketingOutput(null);
		p.setOutput(b);
		f.getContentPane().add(p);
		f.pack();
	}

	public BracketingOutput(PlotCanvas p) {
		this.p = p;
		init();
	}
	
	private void init() {
		setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		setPreferredSize(new Dimension(256, 256));

		btnPlay = new JButton("\u25BA");
		btnPlay.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				do_btnPlay_actionPerformed(e);
			}
		});

		btnStop = new JButton("\u25A0");
		btnStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				do_btnStop_actionPerformed(e);
			}
		});

		scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		
		spnDelay = new JSpinner();
		spnDelay.setToolTipText("Delay");
		spnDelay.setModel(new SpinnerNumberModel(new Integer(750), new Integer(10), null, new Integer(10)));
		
		chckbxLoops = new JCheckBox("Loop");
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(spnDelay, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(chckbxLoops)
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(btnPlay)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnStop)))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnStop)
						.addComponent(btnPlay)
						.addComponent(spnDelay, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(chckbxLoops))
					.addContainerGap())
		);

		outputTable = new OutputTable(p);
		scrollPane.setViewportView(outputTable);
		setLayout(groupLayout);
	}

	@Override
	public void repaint() {
		if (outputTable != null)
			if (!isPlaying) {
				outputTable.play();
				btnPlay.setText("\u2016");
				isPlaying = true;
			} else {
				outputTable.pause();
				btnPlay.setText("\u25BA");
				isPlaying = false;
			}
		super.repaint();
	}

	protected void do_btnPlay_actionPerformed(ActionEvent e) {
		if (!isPlaying) {
			outputTable._interval = (Integer) spnDelay.getValue();
			outputTable._loops = chckbxLoops.isSelected();
			outputTable.play();
			btnPlay.setText("\u2016");
			isPlaying = true;
		} else {
			outputTable.pause();
			btnPlay.setText("\u25BA");
			isPlaying = false;
		}
		if (isPlaying && !outputTable._loops && outputTable.getSelectedRow() == outputTable.getRowCount() - 1)
			do_btnPlay_actionPerformed(null);
	}

	protected void do_btnStop_actionPerformed(ActionEvent e) {
		outputTable.stop();
		btnPlay.setText("\u25BA");
		isPlaying = false;
	}
	

	public void setOutput(Bracketing b) {
		outputTable = new OutputTable(p);
		outputTable.init(b.header(), b.values(), b.fromTo());
		scrollPane.setViewportView(outputTable);
	}
}
